<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>main</title>
    <link rel="stylesheet" type="text/css" href="{{$smarty.const.JS}}layui/css/layui.css" />
    <style type="text/css">
        form{margin-top: 20px;}
    </style>
    <script type="text/javascript" src="{{$smarty.const.JS}}forbidRight.js"></script>
</head>
<body>
    <form class="layui-form" action="">
        {{if $smarty.get.act == "edit"}} <input type="hidden" name="id" value="{{$menu.id}}">{{/if}}
        <div class="layui-form-item">
            <label class="layui-form-label">菜单名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input" style="width: 60%;" {{if $smarty.get.act == "edit"}} value="{{$menu.name}}"{{/if}}>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否显示</label>
            <div class="layui-input-block">
                <input type="checkbox" {{if $smarty.get.act=="add"}} checked{{/if}} {{if $smarty.get.act=="edit" && $menu.play=="on"}} checked{{/if}} name="play" lay-skin="switch" lay-filter="switchTest" lay-text="显示|隐藏">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">一级菜单</label>
            <div class="layui-input-block">
                <input type="radio" name="first" value="1" title="是" lay-filter="first" {{if $smarty.get.act=="add"}} checked{{/if}} {{if $smarty.get.act=="edit" && $menu.first=="1"}} checked{{/if}}>
                <input type="radio" name="first" value="0" title="否" lay-filter="first" {{if $smarty.get.act=="edit" && $menu.first=="0"}} checked{{/if}}>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">所属菜单</label>
                <div class="layui-input-inline">
                    <select name="pid" class="pid">
                        <option value="">请选择</option>
                        {{if $smarty.get.act == "edit" && $menu.first=="0"}}
                            {{foreach $first as $v}}
                                <option value="{{$v.id}}" {{if $v.id == $menu.pid}} selected="selected"{{/if}}>{{$v.name}}</option>
                            {{/foreach}}
                        {{/if}}
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">控制器名称</label>
            <div class="layui-input-block">
                <input type="text" name="controller" lay-verify="title" autocomplete="off" placeholder="一级菜单无需填写控制器名" class="layui-input cont" style="width: 190px;" {{if $smarty.get.act == "edit"}} value="{{$menu.controller}}"{{/if}} disabled >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单图标</label>
            <div class="layui-input-block icon">
                <input type="text" name="icon" autocomplete="off" placeholder="可不填" class="layui-input" style="width: 190px;display: inline-block;margin-right: 10px;" {{if $smarty.get.act == "edit"}} value="{{$menu.icon}}"{{/if}}>
                <a href="icon" target="_blank">查看图标</a>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
            {{if $smarty.get.act=="add"}}
                <button class="layui-btn" lay-submit="" lay-filter="add">添加</button>
            {{else if $smarty.get.act=="edit"}}
                <button class="layui-btn" lay-submit="" lay-filter="edit">确认修改</button>
            {{/if}}
            </div>
        </div>
    </form>
</body>
<script src="{{$smarty.const.JS}}jquery.min.js" charset="utf-8"></script>
<script src="{{$smarty.const.JS}}layui/layui.js" charset="utf-8"></script>
<script type="text/javascript">
    layui.use('form', function(){
        var form = layui.form;

        form.on('radio(first)', function(data){
            if (data.value == 0) {
                var html = "";
                {{foreach $first as $v}}
                    html += '<option value="{{$v.id}}">{{$v.name}}</option>';
                {{/foreach}}
                $(".pid").append(html);
                $(".cont").attr("placeholder","请填写控制器名");
                $(".cont").attr("disabled",false);
                form.render();//重新渲染
            }else if (data.value == 1) {
                $(".pid").children().remove();
                var html = '<option value="0">请选择</option>';
                $(".pid").append(html);
                $(".cont").attr("placeholder","一级菜单无需填写控制器名");
                $(".cont").attr("disabled",true);
                form.render();//重新渲染
            }
            // console.log(data.value); //被点击的radio的value值
        });
        //监听提交
        form.on('submit(add)', function(data){
            $.ajax({
                url:'add',
                data:data.field,
                type:'post',
                dataType:'json',
                success:function(redata){
                    if (redata.msg == 1) {
                        window.parent.location.reload();//刷新父页面
                        layer.closeAll('iframe');//关闭弹窗
                    }else{
                        layer.alert("添加失败，请重试！");
                    }
                }
            });
            return false;
        });
        form.on('submit(edit)', function(data){
            $.ajax({
                url:'edit',
                data:data.field,
                type:'post',
                dataType:'json',
                success:function(redata){
                    if (redata.msg == 1) {
                        window.parent.location.reload();//刷新父页面
                        layer.closeAll('iframe');//关闭弹窗
                    }else{
                        layer.alert("修改失败，请重试！");
                    }
                }
            });
            return false;
        });
    });
</script>
</html>